﻿/**
 * ...
 * @author 
 */

package loader;
import client.Category;

class CategoryXmlParser 
{
private var xml : Xml;

	public function new(xml : Xml) 
	{
		this.xml = xml;
	}
	
	public function parse() : Array<Category>
	{
		var categorysArray = new Array<Category>();
				
		var responseIterator = xml.iterator();
		if(responseIterator != null ) responseIterator.next();
				
		var categorysIterator = responseIterator.next().elementsNamed("category");
				
		while (categorysIterator.hasNext()) 
		{
			var categoryXml = categorysIterator.next();
			
			var id 		: UInt	 = 0;
			var name 	: String = "";
			
			var attrIterator = categoryXml.elements();
			
			while (attrIterator.hasNext()) 
			{
				var attribute = attrIterator.next();
				var attrName = attribute.nodeName;
				var attrValue = attribute.firstChild().nodeValue;
				
				switch (attrName) 
				{
					case "id":			id = Std.parseInt(attrValue);
					case "name":		name = attrValue;
					
				}				
			}
			
			var category = new Category(
										name,  
										id
									);
										
			categorysArray.push(category);
		}		
		
		return categorysArray;
	}
}